﻿package ua.mihaylenko.app.ui.window 
{
	import caurina.transitions.Equations;
	import caurina.transitions.Tweener;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import ua.mihaylenko.app.controller.APPStage;
	import ua.mihaylenko.app.interfaces.IDispObj;
	/**
	 * ...
	 * @author Mihaylenko A.L.
	 */
	public class UIWindow extends Sprite implements IDispObj
	{
		/**
		 * ALIGN FOR SHOW/HIDE WINDOW(SHOW_HIDE)
		 */
		static public const LEFT_LEFT:int = 0;
		static public const LEFT_RIGHT:int = 1;
		static public const LEFT_BOTTOM:int = 2;
		static public const LEFT_TOP:int = 3;
		static public const LEFT_CENTER:int = 4;
		static public const RIGHT_RIGHT:int = 5;
		static public const RIGHT_LEFT:int = 6;
		static public const RIGHT_BOTTOM:int = 7;
		static public const RIGHT_TOP:int = 8;
		static public const RIGHT_CENTER:int = 9;
		static public const TOP_TOP:int = 10;
		static public const TOP_LEFT:int = 11;
		static public const TOP_RIGHT:int = 12;
		static public const TOP_BOTTOM:int = 13;
		static public const TOP_CENTER:int = 14;
		static public const BOTTOM_BOTTOM:int = 15;
		static public const BOTTOM_LEFT:int = 16;
		static public const BOTTOM_RIGHT:int = 17;
		static public const BOTTOM_TOP:int = 18;
		static public const BOTTOM_CENTER:int = 19;
		static public const CENTER_CENTER:int = 20;
		static public const CENTER_LEFT:int = 21;
		static public const CENTER_RIGHT:int = 22;
		static public const CENTER_BOTTOM:int = 23;
		static public const CENTER_TOP:int = 24;
		static public const MAX_ALIGN:int = 25;
		/////////////////////////////////////////////////
		
		/**
		 * PARCE FOR SHOW AND HIDE PARAMS
		 */
		static private const LEFT:int = 0;
		static private const RIGHT:int = 1;
		static private const BOTTOM:int = 2;
		static private const TOP:int = 3;
		static private const CENTER:int = 4;
		////////////////////////////////////////
		private var _iAlign:int;
		private var _nMaxBlur:Number;
		private var _nCurrentBlur:Number;
		private var _iShow:int;
		private var _iHide:int;
		private var _iTimeShow:int;
		private var _iTimeHide:int;
		private var _cContainer:DisplayObjectContainer;
		private var _aCurrentFilters:Array;
		private var $dataOfClose:*;
		public function UIWindow() 
		{
			
		}
		/**
		 * Show window(whith effect from... to center and blur from max to 0)
		 * when tween complete then execute addedEventListeners function
		 */
		public function show():void {
			APPStage.lockStage = true;
			blurX = _nMaxBlur;
			switch(_iShow) {
				case LEFT:
					y = (APPStage.gameResolution.y - height) / 2;
					x = -width;
					break;
				case RIGHT:
					y = (APPStage.gameResolution.y - height) / 2;
					x = APPStage.gameResolution.x + width;
					break;
				case TOP:
					y = - height;
					x = (APPStage.gameResolution.x - width) / 2;
					break;
				case CENTER:
					y = (APPStage.gameResolution.y - height) / 2;
					x = (APPStage.gameResolution.x - width) / 2;
					scaleX  = scaleY = .1;
					Tweener.addTween(this, { blurX:0, scaleX:1, scaleY:1, useFrames:true, time:_iTimeShow, transition:Equations.easeOutQuint, onComplete:addEventListeners } );
					return;
					break;
				case BOTTOM:
					y = APPStage.gameResolution.y + height;
					x = (APPStage.gameResolution.x - width) / 2;
					break;
			}
			var oTween:Object = { };
			oTween.blurX = 0;
			oTween.x = (APPStage.gameResolution.x - width) / 2;
			oTween.y = (APPStage.gameResolution.y - height) / 2;
			oTween.onComplete = addEventListeners;
			oTween.useFrames = true;
			oTween.transition = Equations.easeOutQuint;
			oTween.time = _iTimeShow;
			APPStage.appStage.addChild(this);
			Tweener.addTween(this, oTween);
		}
		public function hide(event:Event = null):void {
			Tweener.removeTweens(this);
			var oTween:Object = { };
			oTween.blurX = _nMaxBlur;
			oTween.onComplete = removeEventListeners;
			oTween.useFrames = true;
			oTween.transition = Equations.easeOutQuint;
			oTween.time = _iTimeHide;
			switch(_iHide) {
				case LEFT:
					oTween.y = (APPStage.gameResolution.y - height) / 2;
					oTween.x = -width;
					break;
				case RIGHT:
					oTween.y = (APPStage.gameResolution.y - height) / 2;
					oTween.x = APPStage.gameResolution.x + width;
					break;
				case TOP:
					oTween.y = - height;
					oTween.x = (APPStage.gameResolution.x - width) / 2;
					break;
				case CENTER:
					oTween.scaleX = oTween.scaleY = .1;
					break;
				case BOTTOM:
					oTween.y = APPStage.gameResolution.y + height;
					oTween.x = (APPStage.gameResolution.x - width) / 2;
					break;
			}
			Tweener.addTween(this, oTween);
		}
		public function set blurX(nValue:Number):void {
			_nCurrentBlur = nValue;
			var aCurrentFilters:Array = _aCurrentFilters?_aCurrentFilters.slice():[];
			aCurrentFilters.push(new BlurFilter(_nCurrentBlur, _nCurrentBlur, 3));
			filters = aCurrentFilters;
		}
		public function get blurX():Number { return _nCurrentBlur; }
		/* INTERFACE ua.mihaylenko.app.interfce.IDispObj */
		/**
		 * 
		 * @param	...arguments
		 * [0] - $align(Show and hide parameter align)::int - public constants align in this class
		 * [1] - $timeShow(Time for show window)::int - frames
		 * [2] - $timeHide(Time for hide window)::int - frames
		 * [3] - $maxBlur - max blur in show and hide effect
		 */
		public function innit(arguments:Array):void
		{
			if (arguments.length < 4) {
				APPStage.error(toString() + "error innit, parameters not set! " + arguments);
				return ;
			}
			_aCurrentFilters = filters;
			_iTimeShow = arguments[1];
			_iTimeHide = arguments[2];
			_nMaxBlur = arguments[3];
			_iAlign = arguments[0] >= MAX_ALIGN || arguments[0] < 0?Math.random() * MAX_ALIGN:arguments[0];
			if (_iAlign == LEFT_LEFT || _iAlign == LEFT_BOTTOM || _iAlign == LEFT_CENTER || _iAlign == LEFT_RIGHT || _iAlign == LEFT_TOP) {
				_iShow = LEFT;
			}else if (_iAlign == RIGHT_BOTTOM || _iAlign == RIGHT_CENTER || _iAlign == RIGHT_LEFT || _iAlign == RIGHT_RIGHT || _iAlign == RIGHT_TOP) {
				_iShow = RIGHT;
			}else if (_iAlign == TOP_BOTTOM || _iAlign == TOP_CENTER || _iAlign == TOP_LEFT || _iAlign == TOP_RIGHT || _iAlign == TOP_TOP) {
				_iShow = TOP;
			}else if (_iAlign == BOTTOM_BOTTOM || _iAlign == BOTTOM_CENTER || _iAlign == BOTTOM_LEFT || _iAlign == BOTTOM_RIGHT || _iAlign == BOTTOM_TOP) {
				_iShow = BOTTOM;
			}else if (_iAlign == CENTER_BOTTOM || _iAlign == CENTER_CENTER || _iAlign == CENTER_LEFT || _iAlign == CENTER_RIGHT || _iAlign == CENTER_TOP) {
				_iShow = CENTER;
			}
			if (_iAlign == LEFT_LEFT || _iAlign == RIGHT_LEFT || _iAlign == TOP_LEFT || _iAlign == BOTTOM_LEFT || _iAlign == CENTER_LEFT) {
				_iHide = LEFT;
			}else if (_iAlign == RIGHT_RIGHT || _iAlign == LEFT_RIGHT || _iAlign == TOP_RIGHT || _iAlign == BOTTOM_RIGHT || _iAlign == CENTER_RIGHT) {
				_iHide = RIGHT;
			}else if (_iAlign == RIGHT_BOTTOM || _iAlign == LEFT_BOTTOM || _iAlign == TOP_BOTTOM || _iAlign == BOTTOM_BOTTOM || _iAlign == CENTER_BOTTOM) {
				_iHide = BOTTOM;
			}else if (_iAlign == RIGHT_TOP || _iAlign==LEFT_TOP || _iAlign==TOP_TOP || _iAlign== CENTER_TOP || BOTTOM_TOP) {
				_iHide = TOP;
				
			}else if (_iAlign == RIGHT_CENTER || _iAlign == LEFT_CENTER || _iAlign == TOP_CENTER || _iAlign == CENTER_CENTER || _iAlign == BOTTOM_CENTER) {
				_iHide = CENTER;
			}
		}
		public function dispose():void
		{
			if (APPStage.appStage.contains(this)) APPStage.appStage.removeChild(this);
			APPStage.lockStage = false;
			dispatchEvent(new WindowEvent(WindowEvent.WONDOW_CLOSE, $dataOfClose));
		}
		public function addEventListeners():void { 
			filters = _aCurrentFilters?_aCurrentFilters:[]; 
			dispatchEvent(new WindowEvent(WindowEvent.WINDOW_SHOW_COMPLETE));
		}
		public function removeEventListeners():void	{ dispose(); }
		////////////////////////END INTERFACE METHODS//////////////////////
	}

}